import axios from 'axios';
const qs = require('qs');
import Vue from 'vue';
import router from '../../router'
import { Toast ,Indicator} from 'mint-ui';
import {setCookie,getCookie} from './cookieOperate';
let base = '';

//let apiBase = 'http://api.autochat.cc';
let apiBase = 'http://open.autochat.cc/a.php?action=/';

const tagApi = axios.create({
  // baseURL: 'http://116.62.43.17:8080/',
  baseURL: apiBase + '',
  maxRedirects: 0,
  timeout: 30000
});
tagApi.interceptors.request.use(function(config) {
  // Do something before request is sent
  // console.log('config', config);
  const {
      target
  } = config;
  if (target) {
    target.loading = true;
  }
  return config;
}, function(error) {
  // Do something with request error
  return Promise.reject(error);
});
tagApi.interceptors.response.use(function(response) {
  // Do something with response data
  // console.log('response', response);
  const {
      config: {
          target
      }
  } = response;
  if (target) {
    target.loading = false;
  }
  let data = response.data;
  const {
      code = 0
  } = data;
  if (code !== 0) {
    console.error(data);
    return Promise.reject(data);
  }
  return data;
}, function(error) {
  // Do something with response error
  const {
      config: {
          target
      },
      response: {
          status
      } = {
        status: 0
      }
  } = error;
  if (target) {
    target.loading = false;
  }
  // console.log('status', status);
  if (status === 0) {
    error.message = '网络错误，请检查网络后重试。';
  } else if (status === 401) {
    window.sessionStorage.clear();
    window.localStorage.clear();
    window.location.reload();
  }
  console.error(error);
  return Promise.reject(error);
});

export const addUser = params => {
  return axios.post(`${ base }/user/add`, params);
};

let filterparams = (params) => {
  return params;
}
const requestGet = ({
    url
}) => (params, config) => tagApi.get(url, Object.assign({
  params
}, config));

const requestPost = ({
    url
}) => (params, config) => tagApi.post(url, qs.stringify(filterparams(params)), config);

export const login = requestGet({
  url: `${ base }/ops/login/login`
});
export const islogin = requestGet({
  url: `${ base }/ops/index/index`
});
export const isspuer = requestGet({
  url: `${ base }/ops/index/is_spuer`
});
export const datacount = requestGet({
  url: `${ base }/ops/index/data_count`
});
export const authlist = requestGet({
  url: `${ base }/ops/index/auth_list`
});
export const authcarlist = requestGet({
  url: `${ base }/ops/index/auth_car_list`
});
export const authhandle = requestGet({
  url: `${ base }/ops/index/auth_handle`
});
export const complaintlist = requestGet({
  url: `${ base }/ops/index/complaint_list`
});
export const complaint = requestGet({
  url: `${ base }/ops/index/complaint`
});
export const crashlist = requestGet({
  url: `${ base }/ops/crash/crash_list`
});
export const crash = requestGet({
  url: `${ base }/ops/crash/crash`
});
export const userlist = requestGet({
  url: `${ base }/ops/user/user_list`
});
export const operation = requestGet({
  url: `${ base }/ops/user/operation`
});
export const opsuserlist = requestGet({
  url: `${ base }/ops/user/ops_user_list`
});
export const operopsuser = requestGet({
  url: `${ base }/ops/user/oper_ops_user`
});
export const addopsuser = requestGet({
  url: `${ base }/ops/user/add_ops_user`
});
export const logout = requestGet({
  url: `${ base }/ops/login/logout`
});
export const authcount = requestGet({
  url: `${ base }/ops/index/auth_count`
});

//函数方法
//back
export const back = (params) => {
  return router.go(-1);
}

//loading
export const loading = (params) => {
  if(params){
    return Indicator.open({
      spinnerType: 'fading-circle'
    })
  }else{
    return Indicator.close()
  }

}



Vue.filter('apiPath', function(value) {
  return apiBase + apiPathDic[value || ''] || '';
});
